////////////////////////////////////////////////////////////////////////////////
//Copyright (c) 2010 Michal Szczepanski - (http://vane.pl)
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
////////////////////////////////////////////////////////////////////////////////
package pl.vane.as3relax
{
	import flash.events.EventDispatcher;
	
	import pl.vane.net.services.IURLResponder;
	
	/**
	 *  Dispatched when data is returned from the CouchDB API,
	 * 
	 *  @eventType pl.vane.as3relax.CouchEvent.DATA
	 * 
	 * @langversion ActionScript 3.0
	 * @playerversion Flash 9.0
	 * @tiptext
	 */
	[Event(name="couchevent.data", type="pl.vane.as3relax.CouchEvent")]
	
	
	/**
	 *  Dispatched when errors ocures when performing action to the CouchDB API
	 * 
	 *  @eventType pl.vane.as3relax.CouchEvent.FAULT
	 * 
	 * @langversion ActionScript 3.0
	 * @playerversion Flash 9.0
	 * @tiptext
	 */
	[Event(name="couchevent.fault", type="pl.vane.as3relax.CouchEvent")]
	
	
	/**
	 * CouchResponder Class handles response from the CouchDB API
	 * 
	 * <p>CouchResponder implements IURLResponder interface to comunicate with
	 * the CouchDB and dispatch CouchEvent to send the response to your application</p>
	 * 
	 * @see pl.vane.net.services.IURLResponder
	 * @see pl.vane.as3relax.ICouchResponder
	 * 
	 * @langversion ActionScript 3.0
	 * @playerversion Flash 9.0
	 * @tiptext
	 */ 
	public class CouchResponder extends EventDispatcher implements ICouchResponder, IURLResponder
	{
		/**
		 * @private
		 */ 
		protected var event:CouchEvent;
		
		/**
		 * @private
		 */
		protected var _action:String;
		
		/**
		 * Constructor.
		 * 
		 * @langversion ActionScript 3.0
		 * @playerversion Flash 9.0
		 * @tiptext
		 */ 
		public function CouchResponder(action:String = null)
		{
			_action = action;
		}
		
		/**
		 * Returns action performed by the request to the CouchDB API.
		 * 
		 * <p>You can get the action that this responder handle</p>
		 * 
		 * @see pl.vane.as3relax.CouchEventAction 
		 * 
		 * @langversion ActionScript 3.0
		 * @playerversion Flash 9.0
		 * @tiptext
		 */ 
		public function get action():String { return _action; }
		
		/**
		 * Dispatch event with response from the CouchDB API and sends 
		 * the response data in the <code>event.data</code> property.
		 * 
		 * @see pl.vane.net.services.URLLoaderBase
		 * @see pl.vane.net.services.IURLResponder
		 * 
		 * @langversion ActionScript 3.0
		 * @playerversion Flash 9.0
		 * @tiptext
		 */ 
		public function result(result:*):void
		{
			event = new CouchEvent(_action, CouchEvent.DATA);
			event.data = result;
			dispatchEvent(event);
		//	trace(_action, result);
		}
		
		/**
		 * Dispatch fault <code>Array</code> response from the CouchDB API.
		 * the <code>Array</code> has three values:
		 * <ul> 
		 * <li> [1] The full url of the request</li>
		 * <li> [2] The event.text that caused the error with error number and description</li>
		 * <li> [3] The event.type (ioError, securityError) </li>
		 * </ul>
		 * 
		 * @see pl.vane.net.services.URLLoaderBase
		 * @see pl.vane.net.services.IURLResponder
		 * 
		 * @langversion ActionScript 3.0
		 * @playerversion Flash 9.0
		 * @tiptext
		 */ 
		public function fault(fault:Array):void
		{
			event = new CouchEvent(_action, CouchEvent.FAULT);
			event.data = fault;
			dispatchEvent(event);
		//	trace("FAULT:", fault.toString());
		}
	}
}
